<template>
  <div class="songList-table">
    <el-dialog
      :title="!songListForm.id ? '新增' : '修改'"
      :visible.sync="songListAddDialogVisible"
       width="50%" :center="true">
      <el-form :model="songListForm" ref="songListForm" label-width="80px">
          <el-form-item prop="title" label="歌单名" size="mini">
            <el-input v-model="songListForm.title" placeholder="歌单名"></el-input>
          </el-form-item>
        <el-form-item prop="pic" label="图片" size="mini">
          <singleUpload v-model="songListForm.pic" @input="inputHandler" ></singleUpload>
        </el-form-item>
        <el-form-item prop="introduction" label="歌单介绍" size="mini">
          <el-input v-model="songListForm.introduction" placeholder="歌单介绍"></el-input>
        </el-form-item>
        <el-form-item prop="style" label="风格" size="mini">
          <el-input v-model="songListForm.style" placeholder="风格" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" type="warning" @click="songListAddDialogVisible=false">取消</el-button>
        <el-button size="mini"  type="primary" @click="addsongList">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import singleUpload from '@/components/upload/singlePicUpload'
export default {
  name: 'songList-add-or-update',
  components: {
    singleUpload
  },
  data () {
    return {
      songListAddDialogVisible: false,
      songListForm: {
        id: '',
        name: '',
        sex: '',
        birth: '',
        pic: '',
        location: '',
        introduction: ''
      }
    }
  },
  methods: {
    inputHandler (url) {
      console.log(url)
      console.log(1111)
      this.songListForm.pic = url
    },
    init (id) {
      console.log(id)
      this.songListForm.id = id || 0
      this.songListAddDialogVisible = true
      this.$nextTick(() => {
        this.$refs['songListForm'].resetFields()
        if (this.songListForm.id) {
          this.$api.getSongListById({
            params: {id: id}
          }).then((res) => {
            console.log(res.data)
            if (res.data && res.data.code === 200) {
              console.log(22222)
              this.songListForm.title = res.data.data.title
              this.songListForm.pic = res.data.data.pic
              this.songListForm.introduction = res.data.data.introduction
              this.songListForm.style = res.data.data.style
            }
          })
        }
      })
    },
    addsongList () {
      let that = this
      let params = {
        title: this.songListForm.title,
        style: this.songListForm.style,
        pic: this.songListForm.pic,
        introduction: this.songListForm.introduction
      }
      if (!this.songListForm.id) {
        this.$api.addsongList(params).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            that.songListAddDialogVisible = false
            that.$emit('refreshDataList')
          } else {
            console.log(res.data)
          }
        }).catch(error => {
          console.log(error)
        })
      } else {
        params.id = this.songListForm.id
        this.$api.updateSongList(params).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            that.songListAddDialogVisible = false
            that.$emit('refreshDataList')
          } else {
            console.log(res.data)
          }
        }).catch(error => {
          console.log(error)
        })
      }
    }
  }
}
</script>

<style scoped>
  .songList-table{
    margin: auto;
    width: 80%;
  }
  .songList-img{
    width: 100px;
    height: 100px;
    border-radius: 5px;
    overflow: hidden;
  }
  body .el-table th.gutter{
    display: table-cell!important;
  }

</style>
